package com.goods.business.controller;


import com.goods.business.enums.ProductStatusEnum;
import com.goods.business.service.ProductService;
import com.goods.business.service.ProductStockService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author macro
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/business/product")
public class BizProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductStockService productStockService;

    @GetMapping("findProductList")
    public ResponseBean<PageVO<ProductVO>> findProductList(@RequestParam Integer pageNum,
                                                           @RequestParam Integer pageSize,
                                                           @RequestParam(required = false) String name,
                                                           @RequestParam(required = false) Integer categoryId,
                                                           @RequestParam(required = false) String supplier,
                                                           @RequestParam Integer status,
                                                           @RequestParam(required = false) Integer[] categorys
    ) {
        PageVO<ProductVO> list = productService.findProductList(pageNum, pageSize, name, categoryId, supplier, status, categorys);
        return ResponseBean.success(list);
    }

    @PostMapping("add")
    public ResponseBean addProduct(@RequestBody ProductVO productVO) {
        productService.addProduct(productVO);
        return ResponseBean.success();
    }

    @GetMapping("edit/{id}")
    public ResponseBean<ProductVO> editProduct(@PathVariable Integer id) {
        ProductVO productVO = productService.getProductById(id);
        return ResponseBean.success(productVO);
    }

    @PutMapping("update/{id}")
    public ResponseBean<ProductVO> editProduct(@PathVariable Integer id, @RequestBody ProductVO productVO) {
        productService.updateProduct(productVO);
        return ResponseBean.success();
    }

    /**
     * 移到回收站
     *
     * @param id
     * @return
     */
    @PutMapping("remove/{id}")
    public ResponseBean removeProduct(@PathVariable Long id) {
        productService.updateProductStatus(id, ProductStatusEnum.TRASH.getCode());
        return ResponseBean.success();
    }

    /**
     * 删除回收站里的物资
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResponseBean deleteProduct(@PathVariable Long id) {
        productService.deleteProductById(id);
        return ResponseBean.success();
    }

    /**
     * 恢复
     *
     * @param id
     * @return
     */
    @PutMapping("back/{id}")
    public ResponseBean backProduct(@PathVariable Long id) {
        productService.updateProductStatus(id, ProductStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }

    /**
     * 通过
     *
     * @param id
     * @return
     */
    @PutMapping("publish/{id}")
    public ResponseBean publishProduct(@PathVariable Long id) {
        productService.updateProductStatus(id, ProductStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }

    @GetMapping("findProducts")
    public ResponseBean<PageVO<ProductVO>> findProducts(@RequestParam Integer pageNum,
                                                        @RequestParam Integer pageSize,
                                                        @RequestParam Integer status
    ) {
        PageVO<ProductVO> list = productService.findProductList(pageNum, pageSize, null, null, null, status, null);
        return ResponseBean.success(list);
    }


    /**
     * 物资库存模糊搜索
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param categorys
     * @return
     */
    @GetMapping("findProductStocks")
    public ResponseBean<PageVO<ProductStockVO>> findProductStocks(@RequestParam Integer pageNum,
                                                                  @RequestParam Integer pageSize,
                                                                  @RequestParam(required = false) String name,
                                                                  @RequestParam(required = false) Integer[] categorys
    ) throws BusinessException {
        PageVO<ProductStockVO> list = productStockService.findProductStocks(pageNum, pageSize, name, categorys);
        return ResponseBean.success(list);
    }

    /**
     * 饼图
     *
     * @param pageNum
     * @param pageSize
     * @param name
     * @param categorys
     * @return
     * @throws BusinessException
     */
    @GetMapping("findAllStocks")
    public ResponseBean<List<ProductStockVO>> findAllStocks(@RequestParam Integer pageNum,
                                                            @RequestParam Integer pageSize,
                                                            @RequestParam(required = false) String name,
                                                            @RequestParam(required = false) Integer[] categorys
    ) throws BusinessException {
        PageVO<ProductStockVO> list = productStockService.findProductStocks(pageNum, pageSize, name, categorys);
        return ResponseBean.success(list.getRows());
    }
}

